package com.demo.easyExcel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

/**
 * @author ：Howin
 * @Describe POI操作读取文件数据
 * @date ：Created in 2021/7/24 18:50
 */
public class POIRead {
    String Path = "D:\\project\\POI_EasyExcel\\src\\main\\resources\\联系人.xlsx";//07版本
    String Path03 = "D:\\project\\POI_EasyExcel\\src\\main\\resources\\test03.xls";

    //读取Excel数据
    @Test
    public void demo() throws IOException {
        FileInputStream fip = new FileInputStream(Path03);
        Workbook workbook = new HSSFWorkbook(fip);
        //XSSFWorkbook sxssfWorkbook = new XSSFWorkbook(fip);  //07版excel
        //Workbook Workbook = new SXSSFWorkbook(sxssfWorkbook);   //07版excel

        Sheet sheet = workbook.getSheetAt(0);
        //获得行数
        int rowNum = sheet.getLastRowNum();
        for (int i = 0; i < rowNum; i++) {
            //获得列数
            int cellNum = sheet.getRow(i).getPhysicalNumberOfCells();
            for (int j = 0; j < cellNum; j++) {
                double cell = sheet.getRow(i).getCell(j).getNumericCellValue();
                long bits = Double.valueOf(cell).longValue();//解决double类型的数值输出存在小数后边有.0的问题
                System.out.println("bits = " + bits);
            }
        }
        fip.close();
    }
    //一个完整的解析过程
    @Test
    public void POIRead() throws IOException {
        FileInputStream inputStream = new FileInputStream(Path);
        XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = sheets.getSheetAt(0);
        //解析标题内容
        XSSFRow row = sheet.getRow(0);
        int numberOfCells = row.getPhysicalNumberOfCells();//获取该行的有内容的数量
        for (int i = 0; i < numberOfCells; i++) {
            XSSFCell cell = row.getCell(i);
            String value = cell.getStringCellValue();
            System.out.print(value+"     |     ");
        }
        System.out.println();
        //解析具体的内容
        int numberOfRows = sheet.getPhysicalNumberOfRows();//获取行号
        for (int i = 1; i < numberOfCells; i++) {
            XSSFRow row1 = sheet.getRow(i);//获取当前行
            if (row1!=null){
                int ofCells = row1.getPhysicalNumberOfCells();//获取行的列的数量
                for (int j = 0; j < ofCells; j++) {
                    //判断当前单元格的数据类型
                    Cell cell = row1.getCell(j);
                    if (cell != null){
                        int cellType = cell.getCellType();
                        String cellValue = "";

                        switch (cellType){
                            case HSSFCell.CELL_TYPE_STRING://字符串
                                //System.out.println("[String类型]");
                                cellValue=cell.getStringCellValue();
                                break;
                            case HSSFCell.CELL_TYPE_BOOLEAN://布尔类型
                                //System.out.println("[布尔类型]");
                                cellValue=String.valueOf(cell.getBooleanCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_BLANK://为空
                                System.out.println(" [为空] " );
                                break;
                            case HSSFCell.CELL_TYPE_ERROR://存在错误
                                //System.out.println("[错误]");
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC://数据类型:分Data和num
                                //System.out.print("【 NUMERIC】");
                                if (HSSFDateUtil.isCellDateFormatted(cell)) {// 日期
                                    //System.out.print("--【日期】");
                                    Date date = cell.getDateCellValue();
                                    cellValue = new DateTime(date).toString("yyyy-MM-dd");
                                } else {
                                    //不是日期格式，防止数字过长！
                                    //System.out.print("--【转换为字符串输出】");
                                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                    cellValue = cell.toString();
                                }
                                break;
                        }
                        System.out.print(cellValue);
                    }
                }

            }

        }
        inputStream.close();
    }

    @Test
    public void testFormula() throws Exception {
        FileInputStream fis = new FileInputStream("公式.xlsx");
        //创建一个工作簿。使用 excel能操作的这边他都可以操作
        Workbook workbook = new HSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);

        Row row = sheet.getRow(4);
        Cell cell = row.getCell(0);

        //拿到计算公式 evaL
        FormulaEvaluator FormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);

        //输出单元格的内容
        int cellType = cell.getCellType();
        switch (cellType) {
            case Cell.CELL_TYPE_FORMULA://公式
                String formula = cell.getCellFormula();
                System.out.println(formula);

                //计算
                CellValue evaluate = FormulaEvaluator.evaluate(cell);
                String cellValue = evaluate.formatAsString();
                System.out.println(cellValue);
                break;
        }
    }

    @Test
    public void name() {
        double l =1;
        long j = 1;
        System.out.println("l = " + l);
        System.out.println("j = " + j);
    }
}
